package com.producer.service.impl;


import com.producer.queue.bean.DataBean;
import com.producer.queue.dealing.OutPutDealing;
import com.producer.queue.define.TaskDefine;
import com.producer.service.OutPutService;
import com.producer.vo.MsgVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author : liu lin qin
 * @Date :  Created by pangoin on 2019/8/26.
 */
@Component("outPutService")
public class OutPutServiceImpl implements OutPutService {
    private static final Log logger = LogFactory.getLog(OutPutServiceImpl.class);
    @Autowired
    private OutPutDealing outPutDealing;

    private DataBean getDataBean(MsgVO msgVO) {
        DataBean dataBean = new DataBean();
        dataBean.setOutPutDealing(outPutDealing);
        dataBean.setMsg(msgVO.getMsg());
        dataBean.setTopic(msgVO.getTopic());
        dataBean.setPartition(msgVO.getPartition() != null ? msgVO.getPartition() : 1);
        return dataBean;
    }

    @Override
    public void sendOutPut(MsgVO msgVO) {
        do {
            if (TaskDefine.mainQueue.remainingCapacity() < 1) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    logger.error("outPutServiceImpl sendOutPut Thread.sleep error" + e.getMessage());
                }
            } else {
                break;
            }
        } while (true);
        TaskDefine.mainQueue.add(getDataBean(msgVO));
    }
}
